package com.rppowell.pixie.models;

import com.rppowell.pixie.swing.importer.SpriteSetImporterProperties;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PixieProject {

    SpriteLibrary spriteLibrary;
    SpriteSet selectedSpriteSet;
    Sprite selectedSprite;
    MapSet mapSet;
    Map selectedMap;

    public PixieProject() {
        spriteLibrary = new SpriteLibrary();
    }
    public void resetToTestProject() throws Exception {
        spriteLibrary = new SpriteLibrary();

        Graphics2D g;
        Sprite s;

        int w;
        int h;
        SpriteSetProperties properties;
        SpriteSet set;

        w = 16;
        h = 16;
        properties = new SpriteSetProperties("S1", w, h);
        set = new SpriteSet(properties);

        s = new Sprite(w, h, BufferedImage.TYPE_INT_ARGB);
        s.fillRect(null, Color.RED);
        set.addSprite(s);

        s = new Sprite(w, h, BufferedImage.TYPE_INT_ARGB);
        s.fillRect(null, Color.GREEN);
        set.addSprite(s);

        s = new Sprite(w, h, BufferedImage.TYPE_INT_ARGB);
        s.fillRect(null, Color.BLUE);
        set.addSprite(s);

        Map m = set.newMap(3, 4, "S1M1");
        m.setSprite(0, 0, set.get(0));
        m.setSprite(1, 0, set.get(1));
        m.setSprite(2, 0, set.get(2));
        m.setSprite(0, 1, set.get(2));
        m.setSprite(1, 2, set.get(1));
        m.setSprite(2, 3, set.get(0));

        set.newMap(12, 13, "S1M2");
        set.newMap(64, 32, "S1M3");

        spriteLibrary.getSpriteSets().add(set);

        w = 32;
        h = 32;
        properties = new SpriteSetProperties("S2", w, h);
        set = new SpriteSet(properties);

        s = new Sprite(w, h, BufferedImage.TYPE_INT_ARGB);
        s.fillRect(null, Color.CYAN);
        set.addSprite(s);

        s = new Sprite(w, h, BufferedImage.TYPE_INT_ARGB);
        s.fillRect(null, Color.MAGENTA);
        set.addSprite(s);

        s = new Sprite(w, h, BufferedImage.TYPE_INT_ARGB);
        s.fillRect(null, Color.YELLOW);
        set.addSprite(s);

        spriteLibrary.getSpriteSets().add(set);
        
    }

    public SpriteLibrary getSpriteLibrary() {
        return spriteLibrary;
    }
    
    public void resetProject() {
        spriteLibrary = new SpriteLibrary();        
    }

    public void newSpriteSet(SpriteSetProperties properties) {
        SpriteSet set = new SpriteSet(properties);
        Sprite s = new Sprite(properties.getSpriteWidth(), properties.getSpriteHeight(), Sprite.TYPE_INT_ARGB);
        set.add(s);
        spriteLibrary.getSpriteSets().add(set);        
    }
    
    public void newMap(MapProperties m) {
        System.out.println(getClass().toString() + ".newMap - " + m);
        SpriteSet s = m.getSpriteSet();
        s.newMap(m.getMapWidth(), m.getMapHeight(), m.getMapName());
    }
    
    public void importSpriteSet(SpriteSetImporterProperties properties) {
        System.out.println(getClass().toString() + ".importSpriteSet - " + properties);
        if (properties != null) {
            System.out.println(getClass().toString() + " SpriteSetImporterProperties.tileSize " + properties.width + "," + properties.height);
            SpriteSetProperties spriteSetProperties = new SpriteSetProperties(properties.name, properties.width, properties.height);
            SpriteSet set = new SpriteSet(spriteSetProperties);

            for (int j = properties.yoffset; (j + properties.height) < properties.image.getHeight(); j = j + properties.yspacing + properties.height) {
                for (int i = properties.xoffset; (i + properties.width) < properties.image.getWidth(); i = i + properties.xspacing + properties.width) {
                    int x = i;
                    int y = j;
                    Logger.getLogger(getClass().getName()).log(Level.FINEST, getClass().getName()+".importSpriteSet("+properties+") ["+x+","+y+"]");
                    Sprite s = new Sprite(properties.width, properties.height, BufferedImage.TYPE_INT_ARGB);
                    s.setImage(properties.image.getSubimage(x, y, properties.width, properties.height));
                    set.add(s);
                }
            }
            spriteLibrary.getSpriteSets().add(set);
        }

    }
}
